package thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_DaChiZi
 * Date: 2025/6/26
 * Time: 14:24
 */
public class Demo39 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(4);//4个线程
        CountDownLatch countDownLatch = new CountDownLatch(20);// 分成20个任务
        for (int i = 0; i < 20; i++) {
            int id = i;
            executorService.submit(() -> {
                System.out.println("下载任务" + id + "开始执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("下载任务" + id + "执行完毕" );
                countDownLatch.countDown(); // 完毕 over！
            });
        }
        countDownLatch.await(); //等收到20个countdown 结束全部 await = all wait
        System.out.println("所有任务都完成了");
    }
}
